﻿#include "AccusationController.h"

#include <boost/bind.hpp>

using namespace Controllers;

AccusationController::AccusationController(Views::IAccusationView* view, Utils::Logging::ILogger* logger,
	Players::Player* current_player, Cards::CardsCollection cards, Cards::CardSet searched_cards)
	: view(view), current_player(current_player), cards(cards), searched_cards(searched_cards)
{
	this->logger = logger;
	connectEvent<void (bool)>(view->playerDecisionSetEvent,
		boost::bind(&AccusationController::onPlayerDecisionSet, this, _1));
	connectEvent<void (Cards::CardSet)>(view->cardsInAccusationSetEvent,
		boost::bind(&AccusationController::onCardsInAccusationSet, this, _1));
}

AccusationController::~AccusationController()
{
	delete view;
	view = NULL;
}

void AccusationController::perform()
{
	view->askIfPlayerWantsToPerformAccusation();
}

void AccusationController::onPlayerDecisionSet(bool decision)
{
	logger->accusationDecision(decision);

	if (decision)
	{
		view->askForCardsInAccusation(cards);
	}
	else
	{
		accusationNotPerformedEvent();
	}
}

void AccusationController::onCardsInAccusationSet(Cards::CardSet picked_cards)
{
#ifdef _DEBUG
	for (Cards::CardSet::Iterator i = picked_cards.begin(); i != picked_cards.end(); ++i)
	{
		std::clog << __FUNCTION__ << " karta " << (std::string) (*i)->getTitle() << std::endl;
	}
#endif

	if (checkCards(picked_cards))
		accusationCorrectEvent();
	else
		accusationIncorrectEvent();
}


bool AccusationController::checkCards(Cards::CardSet& picked_cards)
{
	if (isAccusationCorrect(picked_cards))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool AccusationController::isAccusationCorrect(Cards::CardSet& picked_cards)
{
	return picked_cards == searched_cards;
}

